package com.guo.hui.config;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName UserInfoService
 * @Description TODO
 * @Author zhengguohui
 * @Date 2018/8/24 17:07
 */

@Service
public class UserInfoService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
        GrantedAuthority grantedAuthority = null;
        User user = null;
        if ("admin".equals(userName)) {
            grantedAuthority = new SimpleGrantedAuthority("ADMIN");
            grantedAuthorities.add(grantedAuthority);
            user = new User("admin", EncryptionUtil.getBCryptData("admin"), grantedAuthorities);
        }
        if ("guohui".equals(userName)) {
            grantedAuthority = new SimpleGrantedAuthority("user_code");
            grantedAuthorities.add(grantedAuthority);
            user = new User("guohui", EncryptionUtil.getBCryptData("guohui"), grantedAuthorities);
        }
        if ("zhaoyun".equals(userName)) {
            grantedAuthority = new SimpleGrantedAuthority("user_add");
            grantedAuthorities.add(grantedAuthority);
            user = new User("zhaoyun", EncryptionUtil.getBCryptData("zhaoyun"), grantedAuthorities);
        }
        return user;
    }


//    @Autowired
//    private MyUserMapper myUserMapper;

//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
////        MyUser myUser = myUserMapper.findByUsername(username);
//        PasswordEncoder encoder = new BCryptPasswordEncoder();
//        String password = encoder.encode("admin");
//        User user = new User("admin", password, AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
//        return user;
//    }
}
